<?php
namespace app\api\model;

use app\admin\event\SystemLog;
use think\Collection;
use think\db\Query;
use think\Model;
use think\Db;

class StatisticsModel extends Base
{

    protected static function init()
    {
        self::observe(SystemLog::class);
    }

    public function getPurchaseList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['supplier'])){
//            $where['p.supplier'] =$data['supplier'];
            $where[] =['p.supplier','=',$data['supplier']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['maker'])){
            $where[] =['p.uid','=',$data['maker']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['p.type1','=',$data['type1']];
        }
        
        $list =Db::name('purchase')->alias('p')
            ->leftJoin('purchaserelation r','r.pid =p.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('supplier s','s.id =p.supplier')
            ->leftJoin('admin a','a.id =p.uid')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store st','st.id =w.store')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id =com.unit')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id =g.color')
            ->leftJoin('color3 cc','cc.id =g.size')
            ->field('p.id,p.number,type1,uid,payment,com.name,item,goods_code,brand,category,unit,purchase_price,g.color,size,
            r.put_number,r.price,r.discount,discount_price,r.money,discount_money,a.name as uid,s.name as supplier,st.name as store,
            c1.name as brand ,c2.name as unit,c.name as category,c3.name as color ,cc.name as size')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single=[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
            switch ($v['type1']){
                case 1:
                    $list['data'][$k]['type1'] ="采购单";
                    break;
                case 2:
                    $list['data'][$k]['type1'] ="生产入库单";
                    break;
                case 3:
                    $list['data'][$k]['type1'] ="调拨单";
                    break;
                case 4:
                    $list['data'][$k]['type1'] ="销售退货单";
                    break;
                default:
                    $list['data'][$k]['type1'] ="配货退货单";
            }
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPurchaseSingleList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['supplier'])){
            $where[] =['p.supplier','=',$data['supplier']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['p.type1','=',$data['type1']];
        }
        $list =Db::name('purchase')->alias('p')
            ->leftJoin('purchaserelation r','r.pid =p.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('supplier s','s.id =p.supplier')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store st','st.id =w.store')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id =g.color')
            ->leftJoin('color3 cc','cc.id = g.size')
            ->field('p.id,payment,com.name,item,goods_code,brand,category,unit,purchase_price,material,season,year,g.color,size,style,
            r.put_number,r.discount,r.price,discount_price,r.money,discount_money,
            c1.name as brand ,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,
            c3.name as color ,cc.name as size')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single =[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPurchaseBillList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['maker'])){
            $where[] =['p.uid','=',$data['maker']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['supplier'])){
            $where[] =['p.supplier','=',$data['supplier']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['p.type1','=',$data['type1']];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        $list =Db::name('purchase')->alias('p')
            ->leftJoin('purchaserelation r','r.pid =p.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('supplier s','s.id =p.supplier')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store st','st.id =w.store')
            ->leftJoin('admin a','a.id =p.uid')
            ->leftJoin('account_type at','at.id =p.type')
            ->leftJoin('set_account sa','sa.id =p.set_account')
            ->field('p.id,p.number,type1,uid,supplier,warehouse,set_account,small,p.type,p.money,total,payment,p.create_time,p.des,
            r.put_number,a.name as uid,s.name as supplier,w.name as warehouse,at.name as type,sa.name as set_account')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single =[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
            switch ($v['type1']){
                case 1:
                    $list['data'][$k]['type1'] ="采购单";
                    break;
                case 2:
                    $list['data'][$k]['type1'] ="生产入库单";
                    break;
                case 3:
                    $list['data'][$k]['type1'] ="调拨单";
                    break;
                case 4:
                    $list['data'][$k]['type1'] ="销售退货单";
                    break;
                default:
                    $list['data'][$k]['type1'] ="配货退货单";
            }
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPurchaseGoodsList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['supplier'])){
            $where[] =['p.supplier','=',$data['supplier']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['p.type1','=',$data['type1']];
        }
        $list =Db::name('purchase')->alias('p')
            ->leftJoin('purchaserelation r','r.pid =p.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('supplier s','s.id =p.supplier')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store st','st.id =w.store')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->field('p.id,payment,com.name,item,goods_code,brand,category,unit,purchase_price,material,season,year,g.style,
            r.put_number,r.price,r.discount,discount_price,r.money,discount_money,
            c1.name as brand ,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single =[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPurchaseSupplierList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['supplier'])){
            $where['p.supplier'] =$data['supplier'];
        }
        if(!empty($data['store'])){
            $where['w.store'] =$data['store'];
        }
        if(!empty($data['status'])){
            $where['p.status'] =$data['status'];
        }
        if(!empty($data['type1'])){
            $where['p.type1'] =$data['type1'];
        }
        $list =Db::name('purchase')->alias('p')
            ->join('purchaserelation r','r.pid =p.id')
            ->join('supplier s','s.id =p.supplier')
            ->join('warehouse w','w.id =p.warehouse')
            ->field('p.id,supplier,payment,r.put_number,discount_money,s.name as supplier')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single =[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPurchaseMakerList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['maker'])){
            $where[] =['p.uid','=',$data['maker']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['p.type1','=',$data['type1']];
        }
        $list =Db::name('purchase')->alias('p')
            ->join('purchaserelation r','r.pid =p.id')
            ->join('warehouse w','w.id =p.warehouse')
            ->join('admin a','a.id =p.uid')
            ->field('p.id,uid,payment,r.put_number,discount_money,a.name as uid')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $str =[];
        $money =[];
        $single =[];
        foreach ($list['data'] as $k=>$v){
            $str[] =$v['id'];
            $money[] =$v['payment'];
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['num'] =count(array_unique($str));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getSaleList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['s.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['kid'])){
            $where[] =['s.kid','=',$data['kid']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['maker'])){
            $where[] =['s.maker','=',$data['maker']];
        }
        if(!empty($data['sales'])){
            $where[] =['s.sales','=',$data['sales']];
        }
        if(!empty($data['set_account'])){
            $where[] =['s.set_account','=',$data['set_account']];
        }
        if(!empty($data['s_store'])){
            $where[] =['s.store','=',$data['s_store']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if($data['number']){
            $where[] =['s.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('store ws','ws.id = w.store')
            ->leftJoin('user','user.id = s.kid')
            ->leftJoin('admin a','a.id = s.maker')
            ->leftJoin('admin sp','sp.id = s.sales')
            ->leftJoin('store ss','ss.id = sp.store')
            ->field('s.id,s.number,type1,sales,kid,warehouse,set_account,total,payment,s.status,s.create_time,s.des,
            r.product_id,put_number,discount,discount_price,r.money,discount_money,com.name,item,goods_code,brand,com.category,
            unit,com.trade_price,purchase_price,material,season,year,g.color,size,style,ws.name as wareStore,ss.name as saleStore,user.name as kid,a.name as maker,
            sp.name as sales,w.name as warehouse,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,
            c6.name as season,c.name as category,c3.name as color,c7.name as size')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $list['data'][$k]['avg'] = $caigou / $num;//采购均价
                }else{
                    $list['data'][$k]['avg'] =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$list['data'][$k]['avg'] * $v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] = round($list['data'][$k]['profit']/($v['put_number']*$v['trade_price']), 2);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] = round(array_sum($rate), 2);
        return $list;
    }

    public function getSaleSingleList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['s.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('store ws','ws.id = w.store')
            ->field('s.id,warehouse,s.status,r.product_id,put_number,discount,discount_price,r.money,discount_money,com.name,item,
            goods_code,brand,com.category,unit,trade_price,purchase_price,material,season,year,g.color,size,style,ws.name as store,
            w.name as warehouse,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,
            c6.name as season,c.name as category,c3.name as color,c7.name as size')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $list['data'][$k]['avg'] = $caigou / $num;//采购均价
                }else{
                    $list['data'][$k]['avg'] =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$list['data'][$k]['avg']*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getSaleGoodsList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['s.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['kid'])){
            $where[] =['s.kid','=',$data['kid']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['maker'])){
            $where[] =['s.maker','=',$data['maker']];
        }
        if(!empty($data['sales'])){
            $where[] =['s.sales','=',$data['sales']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->field('s.id,warehouse,s.status,r.product_id,put_number,discount,discount_price,r.money,discount_money,com.name,item,goods_code,brand,com.category,
            unit,trade_price,purchase_price,material,season,year,g.style,w.name as warehouse,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,
            c6.name as season,c.name as category')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $list['data'][$k]['avg'] = $caigou / $num;//采购均价
                }else{
                    $list['data'][$k]['avg'] =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$list['data'][$k]['avg']*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getSaleBillList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['warehouse'])){
            $where[] =['s.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['kid'])){
            $where[] =['s.kid','=',$data['kid']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['maker'])){
            $where[] =['s.maker','=',$data['maker']];
        }
        if(!empty($data['sales'])){
            $where[] =['s.sales','=',$data['sales']];
        }
        if(!empty($data['s_store'])){
            $where[] =['sp.store','=',$data['s_store']];
        }
        if(!empty($data['set_account'])){
            $where[] =['s.set_account','=',$data['set_account']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if($data['number']){
            $where[] =['s.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('store ws','ws.id = w.store')
            ->leftJoin('user','user.id = s.kid')
            ->leftJoin('admin a','a.id = s.maker')
            ->leftJoin('admin sp','sp.id = s.sales')
            ->leftJoin('store ss','ss.id = sp.store')
            ->leftJoin('set_account sa','sa.id = s.set_account')
            ->leftJoin('account_type at','at.id = s.type')
            ->field('s.id,s.number,s.status,type1,maker,sales,kid,warehouse,set_account,total,small,s.money,s.type,payment,s.create_time,s.des,
            r.put_number,product_id,ws.name as ware_store,ss.name as sale_store,user.name as kid,a.name as maker,sp.name as sales,w.name as warehouse,
            com.purchase_price,trade_price,sa.name as set_account,at.name as type')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $avg = $caigou / $num;//采购均价
                }else{
                    $avg =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$avg * $v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getSaleCustomList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['kid'])){
            $where[] =['s.kid','=',$data['kid']];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('user','user.id = s.kid')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->field('s.id,kid,s.status,r.product_id,put_number,discount_price,user.name as kid,com.purchase_price,trade_price')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $avg = $caigou / $num;//采购均价
                }else{
                    $avg =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$avg*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getSalePersonList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['sales'])){
            $where[] =['s.sales','=',$data['sales']];
        }
        if(!empty($data['s_store'])){
            $where[] =['sp.store','=',$data['s_store']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('store ws','ws.id = w.store')
            ->leftJoin('admin sp','sp.id = s.sales')
            ->leftJoin('store ss','ss.id = sp.store')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->field('s.id,s.status,r.put_number,product_id,discount_price,sp.name as saleName,ws.name as storeName,com.purchase_price,trade_price')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $avg = $caigou / $num;//采购均价
                }else{
                    $avg =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$avg*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getSaleStoreList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['store'])){
            $where[] =['sp.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('admin sp','sp.id = s.sales')
            ->leftJoin('store ss','ss.id = sp.store')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->field('s.id,s.status,r.put_number,product_id,discount_price,ss.name as saleStore,com.purchase_price,trade_price')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $avg = $caigou / $num;//采购均价
                }else{
                    $avg =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$avg*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getWareStoreList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['store'])){
            $where[] =['w.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['type1'])){
            $where[] =['s.type1','=',$data['type1']];
        }
        $list =Db::name('sale')->alias('s')
            ->leftJoin('salerelation r','r.pid =s.id')
            ->leftJoin('warehouse w','w.id = s.warehouse')
            ->leftJoin('store ws','ws.id = w.store')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->field('s.id,s.status,r.put_number,product_id,discount_price,ws.name as wareStore,com.purchase_price,trade_price')
            ->order('s.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $profit =[];
        $rate =[];
        $money =[];
        $nums =[];
        foreach($list['data'] as $k=>$v){
            $num =Db::name('salerelation')->where('product_id',$v['product_id'])->sum('put_number');//所有该单品的总数量
            if($num > 0){
                $caigou =$num*$v['purchase_price'];//所有该单品的采购金额
                if($v['status'] ==1 || 4) {
                    $avg = $caigou / $num;//采购均价
                }else{
                    $avg =$v['purchase_price'];
                }
            }
            if($v['put_number'] > 0){
                $list['data'][$k]['cost'] =$avg*$v['put_number'];//采购成本
                $list['data'][$k]['profit'] =($v['put_number']*$v['trade_price']) - $list['data'][$k]['cost'];//销售毛利
                $profit[] =$list['data'][$k]['profit'];
                $list['data'][$k]['rate'] =$list['data'][$k]['profit']/($v['put_number']*$v['trade_price']);//销售毛利率
                $rate[] =$list['data'][$k]['rate'];
                $money[] =$v['put_number']*$v['trade_price'];
                $nums[] =$v['put_number'];
            }
        }
        $list['saleNum'] =array_sum($nums);
        $list['money'] =array_sum($money);
        $list['profit'] =array_sum($profit);
        $list['rate'] =array_sum($rate);
        return $list;
    }

    public function getInventoryList($data,$field="",$order ='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['i.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if($data['number']){
            $where[] =['i.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['i.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['maker'])){
            $where[] =['i.maker','=',$data['maker']];
        }
        if(!empty($data['state'])){
            $where[] =['r.state','=',$data['state']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['i.status','=',$data['status']];
        }
        $list =Db::name('inventory')->alias('i')
            ->leftJoin('inventory_relation r','r.pid =i.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->leftJoin('admin a','a.id = i.maker')
            ->leftJoin('warehouse w','w.id = i.warehouse')
            ->field('i.id,number,warehouse,i.create_time,r.product_id,r.put_number,loss_num,loss_price,state,
            com.name,item,goods_code,brand,unit,com.category,material,season,year,g.color,size,style,a.name as makerName,w.name as warehouseName,
            c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
            ->order('i.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $goods =[];
        $ware =[];
        $lossNum =[];
        $lossPrice =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $list['data'][$k]['inNum'] =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $ware[] =$v['warehouse'];
            $lossNum[] =$v['loss_num'];
            $lossPrice[] =$v['loss_price'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['goods'] =count(array_unique($goods));
        $list['wareNum'] =count(array_unique($ware));
        $list['lossNum'] =array_sum($lossNum);
        $list['lossPrice'] =array_sum($lossPrice);
        return $list;
    }

    public function getInventoryBillList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['i.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['i.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['status'])){
            $where[] =['i.status','=',$data['status']];
        }
        if(!empty($data['maker'])){
            $where[] =['i.maker','=',$data['maker']];
        }
        if(!empty($data['state'])){
            $where[] =['r.state','=',$data['state']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['i.warehouse','=',$data['warehouse']];
        }
        $list =Db::name('inventory')->alias('i')
            ->leftJoin('inventory_relation r','r.pid =i.id')
            ->leftJoin('admin a','a.id = i.maker')
            ->leftJoin('warehouse w','w.id = i.warehouse')
            ->field('i.id,i.number,maker,warehouse,i.create_time,r.product_id,r.put_number,loss_num,loss_price,state,a.name as makerName,w.name as warehouseName')
            ->order('i.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $goods =[];
        $ware =[];
        $lossNum =[];
        $lossPrice =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $list['data'][$k]['inNum'] =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $ware[] =$v['warehouse'];
            $lossNum[] =$v['loss_num'];
            $lossPrice[] =$v['loss_price'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['goods'] =count(array_unique($goods));
        $list['wareNum'] =count(array_unique($ware));
        $list['lossNum'] =array_sum($lossNum);
        $list['lossPrice'] =array_sum($lossPrice);
        return $list;
    }

    public function getInventoryGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['i.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['state'])){
            $where[] =['r.state','=',$data['state']];
        }
        if(!empty($data['status'])){
            $where[] =['i.status','=',$data['status']];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['g.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $list =Db::name('inventory')->alias('i')
            ->leftJoin('inventory_relation r','r.pid =i.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->field('i.id,warehouse,r.product_id,r.put_number,loss_num,loss_price,state,com.name,item,goods_code,category,brand,unit,
            material,year,season,g.style,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,
            c6.name as season,c.name as category,c3.name as color,c7.name as size')
            ->order('i.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $goods =[];
        $ware =[];
        $lossNum =[];
        $lossPrice =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['inNum'] =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $ware[] =$v['warehouse'];
            $lossNum[] =$v['loss_num'];
            $lossPrice[] =$v['loss_price'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['goods'] =count(array_unique($goods));
        $list['wareNum'] =count(array_unique($ware));
        $list['lossNum'] =array_sum($lossNum);
        $list['lossPrice'] =array_sum($lossPrice);
        return $list;
    }

    public function getInventoryWareList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['i.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['state'])){
            $where[] =['r.state','=',$data['state']];
        }
        if(!empty($data['status'])){
            $where[] =['i.status','=',$data['status']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['i.warehouse','=',$data['warehouse']];
        }
        $list =Db::name('inventory')->alias('i')
            ->leftJoin('inventory_relation r','r.pid =i.id')
            ->leftJoin('warehouse w','w.id =i.warehouse')
            ->field('i.id,warehouse,r.product_id,r.put_number,loss_num,loss_price,state,w.name as warehouse')
            ->order('i.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $goods =[];
        $ware =[];
        $lossNum =[];
        $lossPrice =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['inNum'] =$this->getValue('product_stock',['product_id'=>$v['product_id']],'stock');
            $ware[] =$v['warehouse'];
            $lossNum[] =$v['loss_num'];
            $lossPrice[] =$v['loss_price'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['goods'] =count(array_unique($goods));
        $list['wareNum'] =count(array_unique($ware));
        $list['lossNum'] =array_sum($lossNum);
        $list['lossPrice'] =array_sum($lossPrice);
        return $list;
    }

    public function getAllotList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['a.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['a.number','like','%'.$data['number'].'%'];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['call_in'])){
            $where[] =['a.call_in','=',$data['call_in']];
        }
        if(!empty($data['call_out'])){
            $where[] =['a.call_out','=',$data['call_out']];
        }
        if(!empty($data['status'])){
            $where[] =['a.status','=',$data['status']];
        }
        if(!empty($data['maker'])){
            $where[] =['a.maker','=',$data['maker']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $list =Db::name('allot')->alias('a')
            ->leftJoin('allotrelation r','r.pid =a.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->leftJoin('warehouse w_in','w_in.id =a.call_in')
            ->leftJoin('warehouse w_out','w_out.id =a.call_out')
            ->field('a.id,a.number,call_in,call_out,a.status,a.create_time,a.des,r.put_number as allotNum,product_id,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size,
            w_in.name as inName,w_out.name as outName')
            ->order('a.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $goods =[];
        $single =[];
        $wareIn =[];
        $wareOut =[];
        $Num =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $wareIn[] =$v['call_in'];
            $wareOut[] =$v['call_out'];
            $Num[] =$v['allotNum'];
            $single[] =$v['id'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status'] ="草稿";
                    break;
                case 2:
                    $list['data'][$k]['status'] ="已调拨";
                    break;
                default:
                    $list['data'][$k]['status'] ="已撤销";
            }
        }
        $list['single'] =count(array_unique($single));
        $list['goods'] =count(array_unique($goods));
        $list['wareIn'] =count(array_unique($wareIn));
        $list['wareOut'] =count(array_unique($wareOut));
        $list['num'] =array_sum($Num);
        return $list;
    }

    public function getAllotBillList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['a.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['a.number','like','%'.$data['number'].'%'];
        }
        if($data['des']){
            $where[] =['a.des','like','%'.$data['des'].'%'];
        }
        if(!empty($data['call_in'])){
            $where[] =['a.call_in','=',$data['call_in']];
        }
        if(!empty($data['call_out'])){
            $where[] =['a.call_out','=',$data['call_out']];
        }
        if(!empty($data['status'])){
            $where[] =['a.status','=',$data['status']];
        }
        if(!empty($data['maker'])){
            $where[] =['a.maker','=',$data['maker']];
        }
        $list =Db::name('allot')->alias('a')
            ->leftJoin('allotrelation r','r.pid =a.id')
            ->leftJoin('warehouse w_in','w_in.id =a.call_in')
            ->leftJoin('warehouse w_out','w_out.id =a.call_out')
            ->field('a.id,a.number,call_in,call_out,a.status,a.create_time,maker,a.des,r.put_number as allotNum,product_id,
            w_in.name as call_in,w_out.name as call_out')
            ->order('a.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $wareIn =[];
        $single =[];
        $goods =[];
        $wareOut =[];
        $Num =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $wareIn[] =$v['call_in'];
            $wareOut[] =$v['call_out'];
            $Num[] =$v['allotNum'];
            $single[] =$v['id'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status'] ="草稿";
                    break;
                case 2:
                    $list['data'][$k]['status'] ="已调拨";
                    break;
                default:
                    $list['data'][$k]['status'] ="已撤销";
            }
        }
        $list['single'] =count(array_unique($single));
        $list['goods'] =count(array_unique($goods));
        $list['wareIn'] =count(array_unique($wareIn));
        $list['wareOut'] =count(array_unique($wareOut));
        $list['num'] =array_sum($Num);
        return $list;
    }

    public function getAllotGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['a.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['g.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['status'])){
            $where[] =['s.status','=',$data['status']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $list =Db::name('allot')->alias('a')
            ->leftJoin('allotrelation r','r.pid =a.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->field('a.id,call_in,call_out,r.put_number as allotNum,product_id,com.name,item,goods_code,category,brand,unit,material,season,year,g.style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
            ->order('a.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $wareIn =[];
        $single =[];
        $goods =[];
        $wareOut =[];
        $Num =[];
        foreach($list['data'] as $k=>$v){
            $wareIn[] =$v['call_in'];
            $wareOut[] =$v['call_out'];
            $Num[] =$v['allotNum'];
            $single[] =$v['id'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['single'] =count(array_unique($single));
        $list['goods'] =count(array_unique($goods));
        $list['wareIn'] =count(array_unique($wareIn));
        $list['wareOut'] =count(array_unique($wareOut));
        $list['num'] =array_sum($Num);
        return $list;
    }

    public function getAllotWareList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['a.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['status'])){
            $where[] =['a.status','=',$data['status']];
        }
        $list =Db::name('allot')->alias('a')
            ->leftJoin('allotrelation r','r.pid =a.id')
            ->leftJoin('warehouse wIn','wIn.id =a.call_in')
            ->leftJoin('warehouse wOut','wOut.id =a.call_out')
            ->field('a.id,call_in,call_out,r.put_number as allotNum,state,product_id,wIn.name as call_in,wOut.name as call_out')
            ->order('a.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $wareIn =[];
        $single =[];
        $wareOut =[];
        $Num =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['in_warehouse'] = $v['call_in'];
            $list['data'][$k]['out_warehouse'] = $v['call_out'];
            $wareIn[] =$v['call_in'];
            $wareOut[] =$v['call_out'];
            $Num[] =$v['allotNum'];
            $single[] =$v['id'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['single'] =count(array_unique($single));
        $list['goods'] =count(array_unique($goods));
        $list['wareIn'] =count(array_unique($wareIn));
        $list['wareOut'] =count(array_unique($wareOut));
        $list['num'] =array_sum($Num);
        return $list;
    }

    public function getProductionList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['keeper'])){
            $where[] =['p.keeper','=',$data['keeper']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $list =Db::name('production')->alias('p')
            ->leftJoin('production_relation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('keeper k','k.id =w.person')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->field('p.id,warehouse,p.number,p.status,p.create_time,warehouse,keeper,p.des,r.put_number,product_id,w.name as warehouse,
            k.name as keeper,com.name,item,goods_code,trade_price,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $ware =[];
        $money =[];
        $goods =[];
        $single =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $sh =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $price =$this->getValue('commodity',['id'=>$sh],'trade_price');
            $ware[] =$v['warehouse'];
            $money[] =$v['put_number'] * $price;
            $goods[] =$sh;
            $single[] =$v['put_number'];
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status'] ="草稿";
                    break;
                case 2:
                    $list['data'][$k]['status'] ="已入库";
                    break;
                case 3:
                    $list['data'][$k]['status'] ="已撤销";
                    break;
                default:
                    $list['data'][$k]['status'] ="待入库";
            }
        }
        $list['single'] =array_sum($single);
        $list['goods'] =count(array_unique($goods));
        $list['ware'] =count(array_unique($ware));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getProductionBillList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        if($data['des']){
            $where[] =['p.des','like','%'.$data['des'].'%'];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['keeper'])){
            $where[] =['p.keeper','=',$data['keeper']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('production')->alias('p')
            ->leftJoin('production_relation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('keeper k','k.id =w.person')
            ->field('p.id,warehouse,p.number,p.status,p.create_time,warehouse,keeper,p.des,r.put_number,product_id,w.name as warehouse,
            k.name as keeper')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $ware =[];
        $money =[];
        $goods =[];
        $single =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $sh =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $price =$this->getValue('commodity',['id'=>$sh],'trade_price');
            $ware[] =$v['warehouse'];
            $money[] =$v['put_number'] * $price;
            $goods[] =$sh;
            $single[] =$v['put_number'];
            switch ($v['status']){
                case 1:
                    $list['data'][$k]['status'] ="草稿";
                    break;
                case 2:
                    $list['data'][$k]['status'] ="已入库";
                    break;
                case 3:
                    $list['data'][$k]['status'] ="已撤销";
                    break;
                default:
                    $list['data'][$k]['status'] ="待入库";
            }
        }
        $list['single'] =array_sum($single);
        $list['goods'] =count(array_unique($goods));
        $list['ware'] =count(array_unique($ware));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getProductionGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['g.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $list =Db::name('production')->alias('p')
            ->leftJoin('production_relation r','r.pid =p.id')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->field('p.id,warehouse,r.put_number,product_id,com.name,item,goods_code,category,brand,unit,material,season,year,
            g.style,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $ware =[];
        $money =[];
        $goods =[];
        $single =[];
        foreach($list['data'] as $k=>$v){
            $sh =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $price =$this->getValue('commodity',['id'=>$sh],'trade_price');
            $ware[] =$v['warehouse'];
            $money[] =$v['put_number'] * $price;
            $goods[] =$sh;
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['goods'] =count(array_unique($goods));
        $list['ware'] =count(array_unique($ware));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getProductionWareList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('production')->alias('p')
            ->leftJoin('production_relation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->field('p.id,warehouse,r.put_number,product_id,w.name as warehouse')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $ware =[];
        $money =[];
        $goods =[];
        $single =[];
        foreach($list['data'] as $k=>$v){
            $sh =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
            $price =$this->getValue('commodity',['id'=>$sh],'trade_price');
            $ware[] =$v['warehouse'];
            $money[] =$v['put_number'] * $price;
            $goods[] =$sh;
            $single[] =$v['put_number'];
        }
        $list['single'] =array_sum($single);
        $list['goods'] =count(array_unique($goods));
        $list['ware'] =count(array_unique($ware));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getPrepareList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['pre_store'])){
            $where[] =['p.store','=',$data['pre_store']];
        }
        if(!empty($data['ware_store'])){
            $where[] =['w.store','=',$data['ware_store']];
        }
        if(!empty($data['keeper'])){
            $where[] =['p.keeper','=',$data['keeper']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store ws','ws.id =w.store')
            ->leftJoin('store s','s.id =p.store')
            ->leftJoin('keeper k','k.id =w.person')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->field('p.id,p.number,warehouse,keeper,p.store,total,p.create_time,p.des,r.product_id,put_number,w.name as warehouse,
            k.name as keeper,com.name,item,goods_code,trade_price,category,brand,unit,material,season,year,g.color,size,style,
            c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,
            c3.name as color,c7.name as size,s.name as store,ws.name as ware_store')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['total'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareSingleList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['ware_store'])){
            $where[] =['w.store','=',$data['ware_store']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->leftJoin('color3 c3','c3.id = g.color')
            ->leftJoin('color3 c7','c7.id = g.size')
            ->field('p.id,warehouse,r.product_id,put_number,r.money,w.name as warehouse,
            com.name,item,goods_code,trade_price,category,brand,unit,material,season,year,g.color,size,style,
            c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,
            c3.name as color,c7.name as size')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['store'])){
            $where[] =['p.store','=',$data['store']];
        }
        if(!empty($data['ware_store'])){
            $where[] =['w.store','=',$data['ware_store']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
           $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('product g','g.id = r.product_id')
            ->leftJoin('commodity com','com.id = g.commodity_id')
            ->leftJoin('color1 c1','c1.id =com.brand')
            ->leftJoin('color1 c2','c2.id = com.unit')
            ->leftJoin('color1 c4','c4.id = com.material')
            ->leftJoin('color1 c5','c5.id = g.style')
            ->leftJoin('color1 c6','c6.id = com.season')
            ->leftJoin('category c','c.id =com.category')
            ->field('p.id,warehouse,r.product_id,put_number,r.money,w.name as warehouse,
            com.name,item,goods_code,trade_price,category,brand,unit,material,season,year,g.style,
            c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareBillList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where[] =['p.number','like','%'.$data['number'].'%'];
        }
        if(!empty($data['keeper'])){
            $where[] =['p.keeper','=',$data['keeper']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
        }
        if(!empty($data['store'])){
            $where[] =['p.store','=',$data['store']];
        }
        if(!empty($data['ware_store'])){
            $where[] =['w.store','=',$data['ware_store']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->join('preparerelation r','r.pid =p.id')
            ->join('warehouse w','w.id =p.warehouse')
            ->join('keeper k','k.id =p.keeper')
            ->join('store ws','ws.id =w.store')
            ->join('store s','s.id =p.store')
            ->field('p.id,p.number,warehouse,keeper,p.store,total,p.create_time,p.des,r.product_id,put_number,r.money,w.name as warehouse,
            k.name as keeper,ws.name as ware_store,s.name as store')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['create_time'] =date('Y-m-d H:i:s',$v['create_time']);
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareStoreList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['store'])){
            $where[] =['p.store','=',$data['store']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('store s','s.id =p.store')
            ->field('p.id,p.store,r.product_id,put_number,r.money,s.name as store')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareKeeperList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['keeper'])){
            $where[] =['p.keeper','=',$data['keeper']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('keeper k','k.id =p.keeper')
            ->field('p.id,p.keeper,r.product_id,put_number,r.money,k.name as keeper')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getPrepareWareStoreList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['ware_store'])){
            $where[] =['w.store','=',$data['ware_store']];
        }
        if(!empty($data['status'])){
            $where[] =['p.status','=',$data['status']];
        }
        $list =Db::name('prepare')->alias('p')
            ->leftJoin('preparerelation r','r.pid =p.id')
            ->leftJoin('warehouse w','w.id =p.warehouse')
            ->leftJoin('store s','s.id =w.store')
            ->field('p.id,r.product_id,put_number,r.money,s.name as ware_store')
            ->order('p.id desc')
            ->where($where)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $money[] =$v['money'];
            $goods[] =$this->getValue('product',['id'=>$v['product_id']],'commodity_id');
        }
        $list['money'] =array_sum($money);
        $list['goods'] =count(array_unique($goods));
        return $list;
    }

    public function getOutStockList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where1[] =['number','like','%'.$data['number'].'%'];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if($data['des']){
            $where1[] =['des','like','%'.$data['des'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['status'])){
            $where1[] =['status','=',$data['status']];
        }
        if(!empty($data['kid'])){
            $where1[] =['kid','=',$data['kid']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where1[] =['warehouse','=',$data['warehouse']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('outstock',$where1);
        $list1 =[];
        $list =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('sale')->alias('s')
                        ->leftJoin('salerelation r','r.pid =s.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->leftJoin('user','user.id = s.kid')
                        ->field('s.id,type1 as type,s.number,s.status,type1 as type,s.create_time,kid,s.des,r.product_id,put_number,r.number as allotNum,
                        user.arrears,com.name,item,goods_code,brand,com.category,unit,material,season,year,g.color,size,style,user.name as kid,c1.name as brand,
                        c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('s.id desc')
                        ->where($where)->where('s.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                break;
                case 2:
                    $list1 =Db::name('prepare')->alias('p')
                        ->leftJoin('preparerelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('p.id,type,p.number,p.create_time,type,p.des,r.product_id,put_number,r.number as allotNum,
                        com.name,item,goods_code,brand,com.category,unit,material,season,year,g.color,size,style,
                        c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,
                        c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('a.id,type,a.number,a.status,a.create_time,a.des,r.put_number as allotNum,
            com.name,item,goods_code,brand,com.category,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('purchasereturn')->alias('p')
                        ->leftJoin('purchasereturnrelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('p.id,type,p.number,p.status,p.create_time,p.des,r.put_number as allotNum,
            com.name,item,goods_code,brand,com.category,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $kid =[];
        //单据类型 1销售单 2配货单 3调拨单  4采购退货单
        foreach($list as $k=>$v){
            $list[$k]['create_time'] =date('Y-m-d',$v['create_time']);
            switch ($v['type']){
                case 1:
                    $list[$k]['type'] ='销售单';
                    break;
                case 2:
                    $list[$k]['type'] ='配货单';
                    break;
                case 3:
                    $list[$k]['type'] ='调拨单';
                    break;
                default:
                    $list[$k]['type'] ='采购退货单';
            }
            switch ($v['status']){
                case 1:
                    $list[$k]['status'] ='待出库';
                    break;
                case 2:
                    $list[$k]['status'] ='部分出库';
                    break;
                case 3:
                    $list[$k]['status'] ='已出库';
                    break;
                default:
                    $list[$k]['status'] ='已撤销';
            }
            if(!empty($v['put_number'])){
                $put_number[] =$v['put_number'];
            }
            if(!empty($v['allotNum'])){
                $num[] =$v['allotNum'];
            }
            if(isset($v['arrears'])){
                if($v['arrears'] > 0){
                    $kid[] =$v['kid'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($kid));
        return $list;
    }

    public function getOutStockSingleList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['kid'])){
            $where1[] =['kid','=',$data['kid']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where1[] =['warehouse','=',$data['warehouse']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('outstock',$where1);
        $list =[];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('sale')->alias('s')
                        ->leftJoin('salerelation r','r.pid =s.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->leftJoin('user','user.id = s.kid')
                        ->field('s.kid,user.arrears,r.put_number,r.number as allotNum,com.name,item,goods_code,
                        com.category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,c2.name as unit,
                        c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('s.id desc')
                        ->where($where)->where('s.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('prepare')->alias('p')
                        ->leftJoin('preparerelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('p.id,r.put_number,r.number as allotNum,com.name,item,goods_code,category,
                        brand,unit,material,season,year,g.color,size,style,c1.name as brand,c2.name as unit,c4.name as material,
                        c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('a.id,r.put_number as allotNum,com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,
                        c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,
                        c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('purchasereturn')->alias('p')
                        ->leftJoin('purchasereturnrelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('p.id,r.put_number as allotNum,
            com.name,item,goods_code,brand,com.category,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $kid =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])){
                $put_number[] =$v['put_number'];
            }
            if(!empty($v['allotNum'])){
                $num[] =$v['allotNum'];
            }
            if(isset($v['arrears'])){
                if($v['arrears'] > 0){
                    $kid[] =$v['kid'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($kid));
        return $list;
    }

    public function getOutStockGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['kid'])){
            $where1[] =['kid','=',$data['kid']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('outstock',$where1);
        $list =[];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('sale')->alias('s')
                        ->leftJoin('salerelation r','r.pid =s.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('user','user.id = s.kid')
                        ->field('s.kid,r.put_number,r.number as allotNum,com.name,item,user.arrears,
                        goods_code,brand,com.category,unit,material,season,year,g.style,c1.name as brand,
                        c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('s.id desc')
                        ->where($where)->where('s.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('prepare')->alias('p')
                        ->leftJoin('preparerelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number,r.number as allotNum,com.name,item,goods_code,category,brand,unit,material,
                        season,year,g.style,c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,com.name,item,goods_code,category,brand,unit,material,season,year,g.style,
                        c1.name as brand,c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('purchasereturn')->alias('p')
                        ->leftJoin('purchasereturnrelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,brand,com.category,unit,material,season,year,g.style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $kid =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrears'])){
                if($v['arrears'] > 0){
                    $kid[] =$v['kid'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($kid));
        return $list;
    }

    public function getOutStockCustomList($data,$field="",$order='id desc')
    {
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['kid'])){
            $where1[] =['kid','=',$data['kid']];
        }
        $dataval =$this->selectList('outstock',$where1);
        $list =[];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1;
                    $list1 =Db::name('sale')->alias('s')
                        ->leftJoin('salerelation r','r.pid =s.id')
                        ->leftJoin('user','user.id = s.kid')
                        ->field('s.kid,user.arrears,r.put_number,r.number as allotNum,user.name as kid_name')
                        ->order('s.id desc')
                        ->where('s.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('prepare')->alias('p')
                        ->leftJoin('preparerelation r','r.pid =p.id')
                        ->field('r.put_number,r.number as allotNum')
                        ->order('p.id desc')
                        ->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->field('r.put_number as allotNum')
                        ->order('a.id desc')
                        ->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('purchasereturn')->alias('p')
                        ->leftJoin('purchasereturnrelation r','r.pid =p.id')
                        ->field('r.put_number as allotNum')
                        ->order('p.id desc')
                        ->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $kid =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrears'])){
                if($v['arrears'] > 0){
                    $kid[] =$v['kid'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($kid));
        return $list;
    }

    public function getInStockList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['number']){
            $where1[] =['number','like','%'.$data['number'].'%'];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if($data['des']){
            $where1[] =['des','like','%'.$data['des'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['status'])){
            $where1[] =['status','=',$data['status']];
        }
        if(!empty($data['supplier'])){
            $where1[] =['supplier','=',$data['supplier']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where1[] =['warehouse','=',$data['warehouse']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('instock',$where1);
        $list =[];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('purchase')->alias('p')
                        ->leftJoin('purchaserelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->leftJoin('supplier s','s.id = p.supplier')
                        ->leftJoin('warehouse w','w.id = p.warehouse')
                        ->field('p.id,type1 as type,p.number,p.status,p.create_time,supplier,warehouse,p.des,
                        r.put_number,r.number as allotNum,com.name,item,goods_code,brand,com.category,unit,material,season,year,
                        g.color,size,style,s.name as supplier_name,s.arrear,w.name as warehouse,c1.name as brand,c2.name as unit,
                        c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('production')->alias('p')
                        ->leftJoin('production_relation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->leftJoin('warehouse w','w.id = p.warehouse')
                        ->field('p.id,type,p.number,p.status,type,p.create_time,warehouse,p.des,r.put_number as allotNum,
                        com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
                        c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,
                        c3.name as color,c7.name as size,w.name as warehouse')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('a.id,type,a.number,a.status,a.create_time,a.des,r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                    break;
                case 4:
                    $list1 =Db::name('salereturn')->alias('a')
                        ->leftJoin('salereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('a.id,type,a.number,a.status,a.create_time,a.des,r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('preparereturn')->alias('a')
                        ->leftJoin('preparereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('a.id,type,a.number,a.status,a.create_time,a.des,r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    foreach ($list1 as $k1=>$v1){
                        $list1[$k1]['status'] =$v['status'];
                    }
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $arrears =[];
        foreach($list as $k=>$v){
            $list[$k]['create_time'] =date('Y-m-d',$v['create_time']);
            switch ($v['type']){
                case 1:
                    $list[$k]['type'] ='采购单';
                    break;
                case 2:
                    $list[$k]['type'] ='生产入库单';
                    break;
                case 3:
                    $list[$k]['type'] ='调拨单';
                    break;
                case 4:
                    $list[$k]['type'] ='销售退货单';
                    break;
                default:
                    $list[$k]['type'] ='配货退货单';
            }
            switch ($v['status']){
                case 1:
                    $list[$k]['status'] ='待入库';
                    break;
                case 2:
                    $list[$k]['status'] ='部分入库';
                    break;
                case 3:
                    $list[$k]['status'] ='已入库';
                    break;
                default:
                    $list[$k]['status'] ='已撤销';
            }
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrear'])){
                if($v['arrear'] > 0){
                    $arrears[] =$v['supplier'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($arrears));
        return $list;
    }

    public function getInStockSingleList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['supplier'])){
            $where1[] =['supplier','=',$data['supplier']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where1[] =['warehouse','=',$data['warehouse']];
        }
        if(!empty($data['color'])){
            $where[] =['g.color','=',$data['color']];
        }
        if(!empty($data['sizes'])){
            $where[] =['g.size','=',$data['sizes']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('instock',$where1);
        $list =[];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('purchase')->alias('p')
                        ->leftJoin('purchaserelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->leftJoin('supplier s','s.id = p.supplier')
                        ->field('p.supplier,r.put_number,r.number as allotNum,
                        com.name,item,goods_code,brand,com.category,unit,material,season,year,s.arrear,
                        g.color,size,style,s.name as supplier_name,c1.name as brand,c2.name as unit,
                        c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('production')->alias('p')
                        ->leftJoin('production_relation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('r.put_number as allotNum,
                        com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
                        c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,
                        c3.name as color,c7.name as size')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 4:
                    $list1 =Db::name('salereturn')->alias('a')
                        ->leftJoin('salereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('preparereturn')->alias('a')
                        ->leftJoin('preparereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('color3 c3','c3.id = g.color')
                        ->leftJoin('color3 c7','c7.id = g.size')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.color,size,style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category,c3.name as color,c7.name as size')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $arrears =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrear'])){
                if($v['arrear'] > 0){
                    $arrears[] =$v['supplier'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($arrears));
        return $list;
    }

    public function getInStockGoodsList($data,$field="",$order='id desc')
    {
        $where =[];
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['supplier'])){
            $where1[] =['supplier','=',$data['supplier']];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['style'])){
            $where[] =['g.style','=',$data['style']];
        }
        if(!empty($data['material'])){
            $where[] =['com.material','=',$data['material']];
        }
        if(!empty($data['unit'])){
            $where[] =['com.unit','=',$data['unit']];
        }
        if(!empty($data['year'])){
            $where[] =['com.year','=',$data['year']];
        }
        if(!empty($data['season'])){
            $where[] =['com.season','=',$data['season']];
        }
        $dataval =$this->selectList('instock',$where1);
        $list = [];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1:
                    $list1 =Db::name('purchase')->alias('p')
                        ->leftJoin('purchaserelation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->leftJoin('supplier s','s.id = p.supplier')
                        ->field('p.supplier,r.put_number,r.number as allotNum,
                        com.name,item,goods_code,brand,com.category,unit,material,season,year,
                        g.style,s.name as supplier_name,c1.name as brand,c2.name as unit,
                        c4.name as material,c5.name as style,c6.name as season,c.name as category,s.arrear')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2:
                    $list1 =Db::name('production')->alias('p')
                        ->leftJoin('production_relation r','r.pid =p.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,
                        com.name,item,goods_code,category,brand,unit,material,season,year,g.style,c1.name as brand,
                        c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('p.id desc')
                        ->where($where)->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 4:
                    $list1 =Db::name('salereturn')->alias('a')
                        ->leftJoin('salereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('preparereturn')->alias('a')
                        ->leftJoin('preparereturnrelation r','r.pid =a.id')
                        ->leftJoin('product g','g.id = r.product_id')
                        ->leftJoin('commodity com','com.id = g.commodity_id')
                        ->leftJoin('color1 c1','c1.id =com.brand')
                        ->leftJoin('color1 c2','c2.id = com.unit')
                        ->leftJoin('color1 c4','c4.id = com.material')
                        ->leftJoin('color1 c5','c5.id = g.style')
                        ->leftJoin('color1 c6','c6.id = com.season')
                        ->leftJoin('category c','c.id =com.category')
                        ->field('r.put_number as allotNum,
            com.name,item,goods_code,category,brand,unit,material,season,year,g.style,c1.name as brand,
            c2.name as unit,c4.name as material,c5.name as style,c6.name as season,c.name as category')
                        ->order('a.id desc')
                        ->where($where)->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $arrears =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrear'])){
                if($v['arrear'] > 0){
                    $arrears[] =$v['supplier'];
                }
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($arrears));
        return $list;
    }

    public function getInStockSupplierList($data,$field="",$order='id desc')
    {
        $where1 =[];
        if($data['start_time'] && $data['end_time']){
            $where1[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['type'])){
            $where1[] =['type','=',$data['type']];
        }
        if(!empty($data['supplier'])){
            $where1[] =['supplier','=',$data['supplier']];
        }
        $dataval =$this->selectList('instock',$where1);
        $list = [];
        $list1 =[];
        foreach($dataval as $k=>$v){
            switch ($v['type']){
                case 1;
                    $list1 =Db::name('purchase')->alias('p')
                        ->leftJoin('purchaserelation r','r.pid =p.id')
                        ->leftJoin('supplier s','s.id =p.supplier')
                        ->field('p.supplier,r.put_number,r.number as allotNum,s.arrear')
                        ->order('p.id desc')
                        ->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 2;
                    $list1 =Db::name('production')->alias('p')
                        ->leftJoin('production_relation r','r.pid =p.id')
                        ->field('r.put_number as allotNum')
                        ->order('p.id desc')
                        ->where('p.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 3:
                    $list1 =Db::name('allot')->alias('a')
                        ->leftJoin('allotrelation r','r.pid =a.id')
                        ->field('r.put_number as allotNum')
                        ->order('a.id desc')
                        ->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                case 4:
                    $list1 =Db::name('salereturn')->alias('a')
                        ->leftJoin('salereturnrelation r','r.pid =a.id')
                        ->field('r.put_number as allotNum')
                        ->order('a.id desc')
                        ->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
                    break;
                default:
                    $list1 =Db::name('preparereturn')->alias('a')
                        ->leftJoin('preparereturnrelation r','r.pid =a.id')
                        ->field('r.put_number as allotNum')
                        ->order('a.id desc')
                        ->where('a.id',$v['relation_id'])->select();
                    $list =array_merge($list,$list1);
            }
        }
        $put_number =[];
        $num =[];
        $arrears =[];
        foreach($list as $k=>$v){
            if(!empty($v['put_number'])) {
                $put_number[] = $v['put_number'];
            }
            if(!empty($v['allotNum'])) {
                $num[] = $v['allotNum'];
            }
            if(isset($v['arrear'])){
                if($v['arrear'] > 0){
                    $arrears[] =$v['supplier'];
                }
                /*$arr =$this->getValue('supplier',['id'=>$v['supplier']],'arrear');
                if($arr > 0){
                    $arrears[] =$v['supplier'];
                }*/
            }
        }
        $list['put_num'] =array_sum($put_number);
        $list['num'] =array_sum($num);
        $list['arrears'] =count(array_unique($arrears));
        return $list;
    }

    public function getAnalysList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        $where[] =['p.status','in',[2,4]];
        $where1[] =['s.status','in',[1,4]];
        $list =Db::name('commodity')->alias('com')
            ->leftJoin ('product dan','dan.commodity_id =com.id')
            ->leftJoin ('purchaserelation r1','r1.product_id =dan.id')
            ->leftJoin ('purchase p','p.id =r1.pid')
            ->leftJoin ('product_stock wg','wg.product_id =dan.id')
            ->leftJoin ('salerelation r2','r2.product_id =dan.id')
            ->leftJoin ('sale s','s.id =r2.pid')
            ->leftJoin ('color1 c1','c1.id = com.brand')
            ->leftJoin ('category c','c.id =com.category')
            ->field('com.id,com.name,item,goods_code,category,brand,dan.id as product_id,r1.id as cai_id,r1.put_number as caiNum,r1.money as caiMoney,
            r2.id as sale_id,r2.put_number as saleNum,r2.money as saleMoney,wg.stock,c1.name as brand,c.name as category,s.kid')
            ->order('com.id desc')
            ->where($where)->whereOr($where1)->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $money =[];
        $single1 =[];
        $single2 =[];
        $goods =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['caiMoney'] =$v['caiMoney'];
            $list['data'][$k]['saleMoney'] =$v['saleMoney'];
            $single1[] =$v['cai_id'];
            $single2[] =$v['sale_id'];
            $goods[] =$v['id'];
            if(!empty($v['caiNum'])){
                $money[] =$list['data'][$k]['caiMoney'];
            }
            if(!empty($v['kid'])){
                $list['data'][$k]['kid_name'] = UserModel::find($v['kid'])['name'];
            }
        }
        $list['single'] =count(array_unique($single1)) + count(array_unique($single2));
        $list['goods'] =count(array_unique($goods));
        $list['money'] =array_sum($money);
        return $list;
    }

    public function getContrastList($data,$field="",$order='id desc')
    {
        $where =[];
        if($data['start_time'] && $data['end_time']){
            $where[] =['p.create_time','between',[$data['start_time'],$data['end_time']]];
            $where[] =['s.create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if($data['name']){
            $where[] =['com.name','like','%'.$data['name'].'%'];
        }
        if(!empty($data['category'])){
            $where[] =['com.category','=',$data['category']];
        }
        if(!empty($data['brand'])){
            $where[] =['com.brand','=',$data['brand']];
        }
        if(!empty($data['warehouse'])){
            $where[] =['p.warehouse','=',$data['warehouse']];
            $where[] =['s.warehouse','=',$data['warehouse']];
        }
        $where[] =['p.status','in',[2,4]];
        $where1[] =['s.status','in',[1,4]];
        $list =Db::name('commodity')->alias('com')
            ->leftJoin ('product dan','dan.commodity_id =com.id')
            ->leftJoin ('purchaserelation r1','r1.product_id =dan.id')
            ->leftJoin ('purchase p','p.id =r1.pid')
            ->leftJoin ('warehouse wp','wp.id =p.id')
            ->leftJoin ('salerelation r2','r2.product_id =dan.id')
            ->leftJoin ('sale s','s.id =r2.pid')
            ->leftJoin ('warehouse ws','ws.id =s.id')
            ->leftJoin('color3 c3','c3.id = dan.color')
            ->leftJoin('color3 c7','c7.id = dan.size')
            ->field('com.id,com.name,item,dan.color,size,dan.id as dan_id,r1.put_number as caiNum,r1.money as caiMoney,
            r2.put_number as saleNum,r2.money as saleMoney,c3.name as color,c7.name as size,wp.name as caiWare,ws.name as saleWare, s.create_time as sale_time')
            ->order('dan.id desc')
            ->where($where)
            // ->whereOr($where1)
            ->page($data['page'],$data['size'])->paginate($data['size']);
        $list =$list->toArray();
        $caiNum =[];
        $saleNum =[];
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['caiMoney'] =$v['caiMoney'];
            $list['data'][$k]['saleMoney'] =$v['saleMoney'];
            if(!empty($v['caiNum'])){
                $caiNum[] =$v['caiNum'];
            }
            if(!empty($v['saleNum'])){
                $saleNum[] =$v['saleNum'];
            }
        }
        $list['caiNum'] =array_sum($caiNum);
        $list['saleNum'] =array_sum($saleNum);
        return $list;
    }

    public function getRevenueList($data)
    {
        $where =[];
        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $where[] =['create_time','between',[$data['start_time'],$data['end_time']]];
        }
        if(!empty($data['sales'])){
            $where[] =['sales','=',$data['sales']];
        }
        $arr =$this->getselectList('sale','id,payment,set_account,kid',$where);
        $pid =[];
        $pay =[];
        $kid =[];
        $setAccount =[];
        foreach ($arr as $k=>$v){
            $pid[] =$v['id'];
            $pay[] =$v['payment'];
            $setAccount[] =$v['set_account'];
            $kid[] =$v['kid'];
        }
        $list['sale']['sale_total'] =count($arr);
        $list['sale']['sale_payment'] =array_sum($pay);
        $list['sale']['sale_num'] =array_sum(Db::name('salerelation')->whereIn('pid',$pid)->column('put_number'));
        $arr1 =$this->getselectList('collect','id,payment,set_account',$where);
        $pid1 =[];
        $pay1 =[];
        foreach ($arr1 as $k=>$v){
            $pid1[] =$v['id'];
            $pay1[] =$v['payment'];
            $setAccount[] =$v['set_account'];
        }
        $list['coll']['coll_total'] =count($arr1);
        $list['coll']['coll_payment'] =array_sum($pay1);
        $list['coll']['coll_num'] =array_sum(Db::name('collectrelation')->whereIn('pid',$pid1)->column('put_number'));
        $arr2 =$this->getselectList('salereturn','id,payment,set_account,kid',$where);
        $pid2 =[];
        $pay2 =[];
        foreach ($arr2 as $k=>$v){
            $pid2[] =$v['id'];
            $pay2[] =$v['payment'];
            $setAccount[] =$v['set_account'];
            $kid[] =$v['kid'];
        }
        $list['return']['return_total'] =count($arr2);
        $list['return']['return_payment'] =array_sum($pay2);
        $list['return']['return_num'] =array_sum(Db::name('salereturnrelation')->whereIn('pid',$pid2)->column('put_number'));
        $setAccount =array_values(array_unique($setAccount));
        if(count($setAccount) > 0){
            foreach ($setAccount as $k=>$v){
                $list['set_account'][$k]['account'] =$this->getValue('set_account',['id'=>$v],'account');
                $list['set_account'][$k]['weChat'] =array_sum(Db::name('set_account_flow')->where(['set_account_id'=>$v,'payment_mode'=>'微信'])->column('in_amount'));
                $list['set_account'][$k]['ali'] =array_sum(Db::name('set_account_flow')->where(['set_account_id'=>$v,'payment_mode'=>'支付宝'])->column('in_amount'));
                $list['set_account'][$k]['pos'] =array_sum(Db::name('set_account_flow')->where(['set_account_id'=>$v,'payment_mode'=>'POS'])->column('in_amount'));
                $list['set_account'][$k]['xian'] =array_sum(Db::name('set_account_flow')->where(['set_account_id'=>$v,'payment_mode'=>'现金'])->column('in_amount'));
            }
        }else{
            $list['set_account'] =null;
        }
        $kid =array_values(array_unique($kid));
        if(count($kid) > 0){
            foreach ($kid as $k=>$v){
                $user =$this->getOne('user','name,arrears',['id'=>$v]);
                $list['payment'][$k]['customer'] =$user['name'];
                $list['payment'][$k]['arrears'] =(int)$user['arrears'];
                $list['payment'][$k]['repayment'] =array_sum(Db::name('set_account_flow')->where(['customer_id'=>$v,'business_type'=>'客户对账单收款'])->column('in_amount'));
            }
        }else{
            $list['payment'] =null;
        }
        return $list;
    }
}